﻿using National.Bll.IService;
using National.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 会员管理
/// </summary>
namespace National.Bll.Service
{
   public class Member //:IDenpendecy,IMember
    {
        BaseService<User> bas = new BaseService<User>();
        BaseService<User_Address> address = new BaseService<User_Address>();
        //private IMember member;
        //public Member(IMember Imember)
        //{
        //    member = Imember;
        //}


        
         /// <summary>
         /// 显示表数据
         /// </summary>
         /// <returns></returns>
        public List<User> Display(string name, string Time)
        {
            List<User> users;
            Expression<Func<User, bool>> lambdaExpression;
            if (!String.IsNullOrEmpty(name) && !(String.IsNullOrEmpty(Time)))
            {
                lambdaExpression = a => a.RealName == name && a.CreateTime == Convert.ToDateTime(Time);
                users = bas.QueryList(lambdaExpression);
                return users;
            }
            else if (!String.IsNullOrEmpty(name))
            {
                lambdaExpression = a => a.RealName == name;
                users = bas.QueryList(lambdaExpression);
                return users;
            }
            else if (!String.IsNullOrEmpty(Time))
            {
                lambdaExpression = a => a.CreateTime == Convert.ToDateTime(Time);
                users = bas.QueryList(lambdaExpression);
                return users;
            }
            lambdaExpression = a => true;
            users = bas.QueryList(lambdaExpression);
            return users;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Delete(User user)
        {
            Expression<Func<User_Address, bool>> lambdaExpression = a => a.UserId == user.Id;
            List<User_Address> list = address.QueryList(lambdaExpression);
            if (address.BatchDelete(list))
            {
                return bas.Delete(user);
            }
            return false;
        }

        public bool Enable(User user)
        {
            User user1 = new User();
            Expression<Func<User, bool>> lambdaExpression = a => a.Id == user.Id;
            user1 = bas.Query(lambdaExpression);
            user1.Status = user.Status;
            return bas.Update(user1);
        }

        public bool UpdateUser(User user)
        {
            User user1 = new User();
            Expression<Func<User, bool>> lambdaExpression = a => a.Id == user.Id;
            user1 = bas.Query(lambdaExpression);
            user1.UserName = user.UserName;
            user1.RealName = user.RealName;
            user1.UserSex = user.UserSex;
            user1.Phone = user.Phone;
            user1.CreateTime = DateTime.Now;
            return bas.Update(user1);
        }

        public bool Login(User user)
        {
            Expression<Func<User, bool>> lambdaExpression = a => a.UserName == user.UserName && a.Pwd == user.Pwd;
            return bas.Exists(lambdaExpression);

        }
    }
}
